import { MockMethod } from "vite-plugin-mock";
import Mock from "mockjs"; // 引入mockjs
import utils from "../../../src/utils/utils";

const Random = Mock.Random; // Mock.Random 是一个工具类，用于生成各种随机数据

// http://mockjs.com/
export default [
  {
    url: "/test",
    method: "get",
    response: () => {
      return {
        code: 200,
        // 随机名字
        message: "@name",
        data: ["tom", "jerry"],
      };
    },
  },
  {
    url: "/api/user.get",
    method: "post",
    response: () => {
      return {
        code: 200,
        message: "ok",
        data: ["tom", "jerry"],
      };
    },
  },
  {
    url: "/api/product.listing",
    method: "post",
    response: (options) => {
      // console.log("mock post", options)
      // 生成的数据数组
      const rows = [];
      // 自定义生成的记录数
      const total = 112;
      // 计算每页生成的记录数，最后一页数据不足页时处理
      let cnt = options.body.rows;
      if (options.body.rows * options.body.page > total) {
        cnt = total - options.body.rows * (options.body.page - 1);
      }
      // console.log("mock post cnt", cnt)
      for (let i = 0; i < cnt; i++) {
        const template = {
          // 商品种类
          goodsClass: "女装",
          // 商品Id
          goodsId: options.body.rows * (options.body.page - 1) + 1 + i,
          //商品名称
          goodsName: "@ctitle(10)",
          //商品地址
          goodsAddress: "@county(true)",
          //商品等级评价★
          "goodsStar|1-5": "★",
          //商品图片
          goodsImg: "@Image('100x100','@color','小甜甜')",
          //商品售价
          "goodsSale|30-500.2": 30,
          //上架时间
          goodsPubDate: "@now",
          //商品状态
          "goodsState|1": true,
        };
        rows.push(template);
      }

      return {
        code: 200,
        total: total,
        rows: rows,
      };
    },
  },
];
